﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
   public class NhanvienDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Nhanvien1");
        }

        public DataTable Select_Nhanvien()
        {
            return ExecuteQuery("sp_SelectAll_Nhanvien");
        }

        public DataTable Select_Dantoc()
        {
            return ExecuteQuery("sp_SelectAll_Dantoc");
        }

        public DataTable Select_Tongiao()
        {
            return ExecuteQuery("sp_SelectAll_Tongiao");
        }

        public DataTable Select_Hocvan()
        {
            return ExecuteQuery("sp_SelectAll_Hocvan");
        }

        public DataTable Select_Phongban()
        {
            return ExecuteQuery("sp_SelectAll_Phongban");
        }

        public DataTable Select_Mucluong()
        {
            return ExecuteQuery("sp_SelectAll_Mucluong");
        }

        public DataTable Select_Bachoc()
        {
            return ExecuteQuery("sp_SelectAll_Bachoc");
        }

        public DataTable Select_Nam()
        {
            return ExecuteQuery("sp_SelectAll_Nam");
        }

        public DataTable Select_Thongke()
        {
            return ExecuteQuery("sp_SelectAll_Thongke");
        }

        public DataTable Select_Thongke_Theonam(string name)
        {
            var sql = "set dateformat dmy; execute sp_SelectAll_Thongke_Theonam " + name + " ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Select_Nhanvien_All(string manv)
        {
            var sql = "sp_SelectAll_Nhanvien_All '" + manv + "'";
            return ExecuteQuery(sql);
        }

        public DataTable Select_Nhanvien_Luong(string manv)
        {
            var sql = "sp_SelectAll_Nhanvien_Luong '" + manv + "'";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Nhanvien {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Nhanvien)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Nhanvien '{0}', N'{1}', {2}, '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', N'{9}', '{10}', N'{11}', '{12}', '{13}', {14}, '{15}', '{16}', {17}, {18}, '{19}' ";
            sql = string.Format(sql, o.MANHANVIEN, o.HOTEN, o.GIOITINH, o.NGAYSINH.ToString("dd/MM/yyyy"), o.CMND, o.DANTOC, o.TONGIAO, o.QUEQUAN, o.CHOOHIENNAY, o.TRINHDOHOCVAN, o.DIENTHOAI, o.EMAIL, o.NGAYTHAMGIACONGTAC.ToString("dd/MM/yyyy"), o.NGAYNGHICONGTAC.ToString("dd/MM/yyyy"), o.MUCLUONG, o.MABAC, o.MAPHONGBAN, o.HESOLUONG, o.HESOPHUCAP, o.IMAGES );
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Nhanvien)obj;
            var sql = "set dateformat dmy; execute sp_Update_Nhanvien '{0}', N'{1}', {2}, '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', N'{9}', '{10}', N'{11}', '{12}', '{13}', {14}, '{15}', '{16}', {17}, {18}, '{19}' ";
            sql = string.Format(sql, o.MANHANVIEN, o.HOTEN, o.GIOITINH, o.NGAYSINH.ToString("dd/MM/yyyy"), o.CMND, o.DANTOC, o.TONGIAO, o.QUEQUAN, o.CHOOHIENNAY, o.TRINHDOHOCVAN, o.DIENTHOAI, o.EMAIL, o.NGAYTHAMGIACONGTAC.ToString("dd/MM/yyyy"), o.NGAYNGHICONGTAC.ToString("dd/MM/yyyy"), o.MUCLUONG, o.MABAC, o.MAPHONGBAN, o.HESOLUONG, o.HESOPHUCAP, o.IMAGES);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select *, (Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as GIOITINH from tb_Nhanvien";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }       

        public DataTable Search(DAL.Entities.Nhanvien o)
        {
            string sql;
            if (o.MANHANVIEN != "")
                sql = @"Select *, (Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as GIOITINH from tb_Nhanvien Where MANHANVIEN like '%" + o.MANHANVIEN + "%'  ";
            else
                sql = @"Select MANHANVIEN, HOTEN,(Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as GIOITINH, NGAYSINH, SCMND, DANTOC, TONGIAO, QUEQUAN, CHOOHIENNAY, TRINHDOHOCVAN, DIENTHOAI, EMAIL, NGAYTHAMGIACONGTAC, NGAYNGHICONGTAC, MUCLUONG, MABAC, MAPHONGBAN from tb_Nhanvien Where HOTEN like N'" + o.HOTEN + "' or GIOITINH like N'" + o.GIOITINH + "' or NGAYSINH like N'" + o.NGAYSINH + "' or DIENTHOAI like N'" + o.DIENTHOAI + "' or QUEQUAN like N'" + o.QUEQUAN + "' or CHOOHIENNAY like N'" + o.CHOOHIENNAY + "' or MABAC like N'" + o.MABAC + "' or MAPHONGBAN like N'" + o.MAPHONGBAN + "'";

            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MANHANVIEN) as Ma From tb_Nhanvien";
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
